<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

// @filename   Mproduct.php

class Mproduct extends Model {

    function Mproduct() {
        parent::Model();
    }

    function get_product_info($id) {
        $query = $this->db->get_where('products',array('id'=>$id));
        return $query->row_array();
    }

    function get_product_list($type,$limit=10,$offset=0) {
    	$this->db->limit($limit,$offset);
    	$this->db->order_by('porder asc, id desc, add_time desc');
        $query = $this->db->get_where('products',array('p_type'=>$type));
        return $query->result_array();
    }

    function get_more_product($type,$offset=0,$limit=10) {
        $this->db->limit($limit,$offset);
        $query = $this->db->get_where('products',array('p_type'=>$type));
        return $query->result_array();
    }

    function get_product_count($type) {
        $this->db->where(array('p_type'=>$type));
        return $this->db->count_all_results('products');
    }

    function get_hot_products($limit=10){
    	$this->db->limit($limit);
    	$this->db->order_by('buy_times','desc');
    	$this->db->order_by('id desc,add_time desc');
    	$query = $this->db->get('products');
    	return $query->result_array();
    }

    function get_products_group_by_type($limit){
    	$types = self::get_product_types();
    	$ts = array();
    	foreach($types as $type){
    		$ts[] = $type['id'];
    	}
    	$ps = array();
    	foreach ($ts as $t){
    		$ps[] = self::get_product_list($t,$limit);
    	}
    	return $ps;
    }

    function get_product_types(){
    	$query = $this->db->get('product_type');
    	return $query->result_array();
    }

    function add_new_product($values) {
        return $this->db->insert('products',$values);
    }

    function update_product_info($id,$values) {
        $this->db->where('id',$id);
        return $this->db->update('products',$values);
    }

    function delete_product($id) {
        return $this->db->delete('products',array('id'=>$id));
    }

    function add_new_type($values) {
        return $this->db->insert('product_type',$values);
    }

    function get_type_info($id) {
        $query = $this->db->get_where('product_type',array('id'=>$id));
        return $query->row_array();
    }

    function update_type_info($id,$values) {
        return $this->db->update('product_type',$values,array('id'=>$id));
    }

    function delete_type($id){
    	return $this->db->delete('product_type',array('id'=>$id));
    }
    
    function update_pro_times($pid){
    	$this->db->where('id',$pid);
    	$this->db->set('buy_times','buy_times+1',FALSE);
    	return $this->db->update('products');
    }
}
?>